package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;




import view.EditFilterPanel;

import view.ViewHelpers;
import model.AppModel;
import model.Filter;
import model.Folder;
import model.ModelException;


/**
 * The controller for creating or editing templates
 * 
 * This controller has a reference to the main model, and the current view it is working on.
 * It assigns handlers to events for the cancel, update and add buttons and specifies the actions
 * to invoke upon triggering these events. 
 * 
 * @author MUSAB
 *
 * @version $Revision: 1.0 $
 */
public class EditFilterController {
	
	
	private AppModel model;
	private Filter filter;
	private EditFilterPanel view;

	/**
	 * Construction using the application model, the mode (create/update) and the filter object)
	 * 
	 * @param model The application model
	
	 * @param filter The filter object, used if the mode is update
	 */
	public EditFilterController(AppModel model, Filter filter){
		
		this.model = model;
		this.filter = filter;
		setupView();
		assignHandlers();
		showView();
	}

	/**
	 * Assigns handlers to different events on the view
	 */
	private void assignHandlers() {
			
		/**
		 * Cancel
		 */
		view.getCancelBtn().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				actionCancel();
			}
		});
		
		/**
		 * Add
		 */
		view.getAddBtn().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				actionAdd();
			}
		});
		
		/**
		 * update
		 */
		view.getUpdateBtn().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				actionUpdate();
			}
		});
		
	}

	/**
	 * Update the filter
	 */
	protected void actionUpdate() {
		
		String to=view.getTo().getText().trim();
		String from=view.getFrom().getText().trim();
		String body=view.getBody().getText().trim();
		String subject=view.getSubject().getText().trim();
		String into=view.getMoveToFolderComboBox().getSelectedItem().toString();
		String title=view.getTitle().getText().trim();
		
		int folderId = model.getUser().getFolder(into).getId();
		
		if (inputsAreValid(body, from, to,title)){
			filter.setBody(body);
			filter.setFrom(from);
			filter.setTo(to);
			filter.setFolderId(folderId);
			filter.setSubject(subject);
			try {
				filter.save();
			} catch (ModelException e) {
				ViewHelpers.alertError("The filter could not be updated, please try again later");
				e.printStackTrace();
			}
			ViewHelpers.disposeParentDialog(view);
		}
	}

	/**
	 * Add the filter
	 */
	protected void actionAdd() {
		
		String to=view.getTo().getText().trim();
		String from=view.getFrom().getText().trim();
		String body=view.getBody().getText().trim();
		String subject=view.getSubject().getText().trim();
		String into=view.getMoveToFolderComboBox().getSelectedItem().toString();
		String title=view.getTitle().getText().trim();
		
		//find folder id
		Folder folder=model.getUser().getFolder(into);
		int folderId=folder.getId();
		
		 if (inputsAreValid(body, from, to,title)){
			try {	
				model.getUser().createNewFilter(title, to, from, body,subject, folderId);
			} catch (ModelException e) {
				ViewHelpers.alertError("The filter could not be created, please try again later");
				e.printStackTrace();
			}

			ViewHelpers.disposeParentDialog(view);
		}
		
	}

/**
 * Validates the inputs from the user




 * @param body String
 * @param from String
 * @param to String
 * @param title String
 * @return boolean
 */
	private boolean inputsAreValid(String body, String from, String to, String title) {
		
		if (title.isEmpty()){
			ViewHelpers.alertError("No title provided");
			return false;
		}
		
		// Empty
		if (body.isEmpty() && from.isEmpty() && to.isEmpty()){
			ViewHelpers.alertError("No critera provided");
			return false;
		}
		
		// If a new filter, check for existing title
		if (filter == null){
			for (Filter filter : model.getUser().getFilters()){
				if (filter.getTitle().trim().equalsIgnoreCase(title)){
					ViewHelpers.alertError("You already have a filter with the same title, please use a different one");
					return false;
				}
			}
		}

		return true;
	}

	/**
	 * Cancel and close the view
	 */
	protected void actionCancel() {
		ViewHelpers.disposeParentDialog(view);
	}

	/**
	 * Shows the view with the appropriate mode
	 */
	private void showView() {
		
		if (filter == null){
			ViewHelpers.showDialog(view, "Create new filter");
		} else {
			ViewHelpers.showDialog(view, "Edit filter: " + filter.getTitle());
		}
	}

	/**
	 * Create the edit or create view with appropriate settings
	 */
	private void setupView() {

		view = new EditFilterPanel();
		
		if (filter == null){
			
			// New template
			view.setMode("create");
			
		} else {
			
			// Updating existing filter
			view.setMode("update");
			view.setToText(filter.getTo());
			view.setFromText(filter.getFrom());
			view.setBodyText(filter.getBody());
			view.setTitleText(filter.getTitle());
			view.setSubjectText(filter.getSubject());

		}
		
		//put folders in drop down list
		
		List<Folder> folders=model.getUser().getFolders();
		for (Folder f: folders)
		{
			if(f.getName().equalsIgnoreCase("Outbox")||f.getName().equalsIgnoreCase("Drafts")||f.getName().equalsIgnoreCase("Sent"))
				continue;
			view.getMoveToFolderComboBox().addItem(f.getName());
		}
	
	}	
	
}

